package com.academic_system;

import com.academic_system.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@SpringBootApplication
@ControllerAdvice
@EnableAsync
public class AcademicSystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(AcademicSystemApplication.class, args);
    }

    public @ResponseBody
    Result defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        Result result = Result.error(500, "服务器飞走了");
        e.printStackTrace();
        //看看是不是自定义异常
        String msg = e.getMessage();
        if (!StringUtils.isBlank(msg)) {
            String[] list = msg.split(":");
            //只有自定义的异常可以显示内容
            String customExceptionName = "ServerException";
            if (StringUtils.equals(list[0], customExceptionName)) {
                result.setMsg(msg.split(":")[2]);
                result.setCode((Integer.parseInt(msg.split(":")[1])));
            }
        }
        return result;
    }

}
